package com.hundsun.yr.universal.p2p.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.hundsun.mobile.zwgt.R;
import com.hundsun.yr.universal.library.eventbus.EventCenter;
import com.hundsun.yr.universal.library.netstatus.NetUtils;
import com.hundsun.yr.universal.library.utils.RegularUtils;
import com.hundsun.yr.universal.library.widget.HSButton;
import com.hundsun.yr.universal.library.widget.HSEditText;
import com.hundsun.yr.universal.p2p.bean.UserInfo;
import com.hundsun.yr.universal.p2p.config.Constants;
import com.hundsun.yr.universal.p2p.config.Extras;
import com.hundsun.yr.universal.p2p.config.TaskConstants;
import com.hundsun.yr.universal.p2p.presenter.base.LoginPresenter;
import com.hundsun.yr.universal.p2p.presenter.impl.LoginPresenterImpl;
import com.hundsun.yr.universal.p2p.ui.activity.base.BaseActivity;
import com.hundsun.yr.universal.p2p.utils.BaseRequestTool2;
import com.hundsun.yr.universal.p2p.utils.CommTool;
import com.hundsun.yr.universal.p2p.view.LoginView;
import com.hundsun.yr.universal.p2p.widget.ValidateButton;

import org.json.JSONException;
import org.json.JSONObject;

import de.greenrobot.event.EventBus;

/**
 * Created by xx on 2017/5/31.
 * 用于快速登陆
 */
public class QuickLoginActivity extends BaseActivity implements View.OnClickListener,LoginView{
    private HSEditText et_moble;
    private HSEditText et_vcode;
    private ValidateButton btn_vcode;
    private HSButton btn_quick_login;
    private TextView tv_voice_sms;
    private String mobile;
    private String vCode;
    private LoginPresenter mPresenter;
    private String zwgt_data;
    /**
     * 跳转标识
     */
    private String NAVIGATE_TAG = "";

    @Override
    protected boolean isApplyKitKatTranslucency() {
        return true;
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.act_quick_login;
    }

    @Override
    protected void initViewsAndEvents() {
        findView();
        toolbar_title.setText(getString(R.string.quick_login));
        et_moble.setText(mobile);
        btn_vcode.setOnClickListener(this);
        btn_quick_login.setOnClickListener(this);
        tv_voice_sms.setOnClickListener(this);
        mPresenter = new LoginPresenterImpl(mContext,this);
    }

    @Override
    protected void getBundleExtras(Bundle extras) {
        mobile = extras.getString(UserInfo.username);
        NAVIGATE_TAG = extras.getString(Extras.NAVIGATE_TAG,"");
    }

    @Override
    protected void onEveComming(EventCenter eventCenter) {

    }

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected View getLoadingTargetView() {
        return null;
    }

    @Override
    protected void onNetworkConnected(NetUtils.NetType type) {

    }

    @Override
    protected void onNetworkDisConnected() {

    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return true;
    }

    @Override
    protected boolean toggleOverridePendingTransition() {
        return false;
    }

    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        return null;
    }

    /**
     * findView
     */
    private void findView() {
        et_moble = findView(R.id.et_moble);
        et_vcode = findView(R.id.et_vcode);
        btn_vcode = findView(R.id.btn_vcode);
        btn_quick_login = findView(R.id.btn_quick_login);
        tv_voice_sms = findView(R.id.tv_voice_sms);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_quick_login:
                mobile = et_moble.getPhone();
                vCode = et_vcode.getText().toString();
                //准备登陆
                //验证手机号
                if (TextUtils.isEmpty(mobile)){
                    showToast(R.string.empty_phone_number);
                    return;
                }

                if(!RegularUtils.isPhoneNum(mobile)){
                    showToast(R.string.error_phone_number);
                    return ;
                }
                //验证验证码格式
                if (TextUtils.isEmpty(vCode)){
                    showToast(R.string.empty_vcode);
                    return;
                }

                if (!RegularUtils.isLength(vCode,6)){
                    showToast(R.string.error_vcode);
                    return;
                }

                //进行验证码登陆
                zwgt_data = BaseRequestTool2.getFormatedDateString();
                JSONObject map = new JSONObject();
                try {
                    map.put("phone", mobile);
                    map.put("vcode", vCode);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                JSONObject jsonParams = CommTool.getJSONParams(map, mConfigSP.getShareProf(TaskConstants.TASK_CONFIG),zwgt_data);

                mPresenter.loadingData(TaskConstants.TASK_LOGINBYSMS,getString(R.string.root_url_test) + getString(R.string.api_index_test) + TaskConstants.TASK_LOGINBYSMS, Constants.EVENT_DATA,jsonParams,
                        BaseRequestTool2.getHearder(mContext,TaskConstants.TASK_POST,getString(R.string.api_index_test) + TaskConstants.TASK_LOGINBYSMS,"",mConfigSP.getShareProf(TaskConstants.TASK_CONFIG), zwgt_data),false);
                break;
            case R.id.btn_vcode://请求登陆验证码
                mobile = et_moble.getPhone();
                //验证手机号
                if (TextUtils.isEmpty(mobile)){
                    showToast(R.string.empty_phone_number);
                    return;
                }

                if(!RegularUtils.isPhoneNum(mobile)){
                    showToast(R.string.error_phone_number);
                    return ;
                }

                zwgt_data = BaseRequestTool2.getFormatedDateString();
                JSONObject map2 = new JSONObject();
                try {
                    map2.put("type","15");
                    map2.put("phone", mobile);
                    map2.put("channel", "0");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                JSONObject jsonParams2= CommTool.getJSONParams(map2, mConfigSP.getShareProf(TaskConstants.TASK_CONFIG),zwgt_data);

                mPresenter.loadingData(TaskConstants.TASK_SENDSMS,getString(R.string.root_url_test) + getString(R.string.api_index_test) + TaskConstants.TASK_SENDSMS, Constants.EVENT_DATA,jsonParams2,
                        BaseRequestTool2.getHearder(mContext,TaskConstants.TASK_POST,getString(R.string.api_index_test) + TaskConstants.TASK_SENDSMS,"",mConfigSP.getShareProf(TaskConstants.TASK_CONFIG), zwgt_data),false);

                break;

            case R.id.tv_voice_sms://发送语音短信
                mobile = et_moble.getPhone();
                //验证手机号
                if (TextUtils.isEmpty(mobile)){
                    showToast(R.string.empty_phone_number);
                    return;
                }

                if(!RegularUtils.isPhoneNum(mobile)){
                    showToast(R.string.error_phone_number);
                    return ;
                }

                zwgt_data = BaseRequestTool2.getFormatedDateString();
                JSONObject map3 = new JSONObject();
                try {
                    map3.put("type","15");
                    map3.put("phone", mobile);
                    map3.put("channel", "1");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                JSONObject jsonParams3= CommTool.getJSONParams(map3, mConfigSP.getShareProf(TaskConstants.TASK_CONFIG),zwgt_data);

                mPresenter.loadingData(TaskConstants.TASK_SENDSMS,getString(R.string.root_url_test) + getString(R.string.api_index_test) + TaskConstants.TASK_SENDSMS, Constants.EVENT_DATA,jsonParams3,
                        BaseRequestTool2.getHearder(mContext,TaskConstants.TASK_POST,getString(R.string.api_index_test) + TaskConstants.TASK_SENDSMS,"",mConfigSP.getShareProf(TaskConstants.TASK_CONFIG), zwgt_data),false);
                break;
        }
    }



    //-----------------------------------------------------------------
    @Override
    public void initializeViews(String username) {

    }

    @Override
    public void showValidateError(String error) {
        showToast(error);
    }

    @Override
    public void navigateToHomePage() {
        EventBus.getDefault().post(new EventCenter(Constants.EVENT_REFRESH_DATA));//刷新界面的eventBus
        // TODO Auto-generated method stub
        if (TextUtils.isEmpty(NAVIGATE_TAG)) {
            finish();
        } else {
            readyGoThenKill(MainActivity.class);
        }
    }

    @Override
    public void navigateToRegiester() {

    }

    @Override
    public void navigateToJointRegiester() {

    }

    @Override
    public void navigateForgetPassword() {

    }
}
